package com.l.web.common.sequence;

import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
import org.bson.Document;
import org.l.qiao.AbstractCollectionDAO;
import org.l.qiao.Cluster;
import org.l.qiao.builder.Query;
import org.l.qiao.builder.Update;
import org.springframework.stereotype.Repository;

/**
 * Created by 严秋旺 on 2016/11/8.
 */

@Repository
public class SequenceDAO extends AbstractCollectionDAO {

    public static final String COLLECTION = "c_sequence";

    public SequenceDAO() {
        super(COLLECTION);
    }

    public Cluster findAndModify(Query query, Update update) {
        FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
        options.upsert(true);
        options.returnDocument(ReturnDocument.AFTER);
        Document document = getCollection().findOneAndUpdate(new Document(query), new Document(update), options);
        return new Cluster(document);
    }
}
